<?php
/**
 * Created by PhpStorm.
 * User: longyuanmoqi
 * Date: 2016/11/2
 * Time: 20:26
 */
header('Content-type:text/html;charset=utf-8');
$link=mysqli_connect('localhost','root','');
if(!$link){
    die('连接数据库失败！'.mysqli_error($link));
}

mysqli_query($link,'set names utf8');
mysqli_query($link,'use`itcast`');
$sql="select `nickname`,`gender`,`email`,`qq`,`url`,`city`,`skill`,`description` from`userinfo` where`id`=$id";
$rst=mysqli_query($link,$rst);

$data=mysqli_fetch_assoc($rst);
$data['skill']=explode(',',$data['skill']);

$city=array('北京','上海','广州','其他');
$skill=array('HTML','JavaScript','PHP','C++');
$id=$_GET['id'];
if (!empty($_POST)){
    $fields=array('nickname','gender','email','qq','url','city','skill','description');
    foreach ($fields as $v){
        $save_data[$v]=isset($_POST[$v])? $_POST[$v]:'';
    }
}
if ($save_data['gender']!='男'&& $save_data['gender']!='女'){
    die('保存失败：未选择性别。');
}
if ($save_data['city']!='未选择'&&!in_array($save_data['city'],$city)){
    die('保存失败：您填写的城市不在允许的城市列表中。');
}
if (in_array($link,$save_data['skill'])){
    $save_data['skill']=array_intersect($skill,$save_data['skill']);
    $save_data['skill']=implode(',',$save_data['skill']);
}else{
    $save_data['skill']='';
}$sql="update `userinfo` set";
foreach ($save_data as $k=>$v){
    $sql.="`$k`='".mysqli_real_escape_string($link,$v)."',";
}
$sql=rtrim($sql,',')."where id=$id";
$rst=mysqli_query($link,$sql);
echo $rst ? "保存成功：$sql":"保存失败：$sql<br>".mysqli_error($link);

define('APP','itcast');
require 'profile_html.php';